﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Pong
{
    class GC_Racket
    {
        // Atributos (variáveis, substantivos...)

        Texture2D pongRacketImg;
        public Vector2 position;
        public Rectangle size;
        Vector2 velocity;

        float speedAI = 3;


        public int points;

        
        //Métodos (funções, verbos)

        public GC_Racket (Texture2D pongRacketImg, Vector2 position)
        {
            this.pongRacketImg = pongRacketImg;
            this.position = position;
            this.velocity = new Vector2(0, 5);
            this.size = pongRacketImg.Bounds;
            points = 0;
        }

        public void Update(GameWindow window, List<Keys> keys, GC_Ball ball, GC_Racket racketL)
        {
            foreach (Keys key in keys)
            {
                if (Keyboard.GetState().IsKeyDown(key) && key == Keys.Down && position.Y < window.ClientBounds.Height - size.Height)
                {
                    position += velocity;
                }

                if (Keyboard.GetState().IsKeyDown(key) && key == Keys.Up && position.Y >= 0)
                {
                    position -= velocity;
                }

                if (Keyboard.GetState().IsKeyDown(key) && key == Keys.Z && position.Y < window.ClientBounds.Height - size.Height)
                {
                    position += velocity;
                }

                if (Keyboard.GetState().IsKeyDown(key) && key == Keys.A && position.Y >= 0)
                {
                    position -= velocity;
                }
            }

            if (position.Y < 0)
            {
                position.Y = 0;
            }

            if (position.Y > window.ClientBounds.Height - size.Height)
            {
                position.Y = window.ClientBounds.Height - size.Height;
            }

            AI(ball, racketL, window);

            //Atualiza a posição do retângulo da palheta com a sua posição
            this.size.X = (int)position.X;
            this.size.Y = (int)position.Y;
        }

        private void AI(GC_Ball ball, GC_Racket racketL, GameWindow window)
        {
            if (ball.position.X < window.ClientBounds.Width/2)
            {
                if (ball.speed != 0 && (ball.angle > 180 && ball.angle < 270))
                {
                    if (ball.position.Y + ball.size.Height / 2 > racketL.position.Y)
                    {
                        racketL.position.Y += speedAI;
                    }

                    if (ball.position.Y + ball.size.Height / 2 < racketL.position.Y)
                    {
                        racketL.position.Y -= speedAI;
                    }
                }

                if (ball.speed != 0 && (ball.angle > 90 && ball.angle < 180))
                {
                    if (ball.position.Y + ball.size.Height / 2 < racketL.position.Y + racketL.size.Height)
                    {
                        racketL.position.Y -= speedAI;
                    }

                    if (ball.position.Y + ball.size.Height / 2 > racketL.position.Y + racketL.size.Height)
                    {
                        racketL.position.Y += speedAI;
                    }
                }

                if (ball.angle == 180)
                {
                    if (ball.position.Y + ball.size.Height / 2 > racketL.position.Y + racketL.size.Height / 2)
                    {
                        racketL.position.Y += speedAI;
                    }

                    if (ball.position.Y + ball.size.Height / 2 < racketL.position.Y + racketL.size.Height / 2)
                    {
                        racketL.position.Y -= speedAI;
                    }
                }
            }

            // Voltar à posição inicial quando a bola sai de campo.
            if (ball.speed == 0)
            {
                if (ball.position.Y + ball.size.Height / 2 > racketL.position.Y + racketL.size.Height / 2)
                {
                    racketL.position.Y += 2;
                }

                if (ball.position.Y + ball.size.Height / 2 < racketL.position.Y + racketL.size.Height / 2)
                {
                    racketL.position.Y -= 2;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(pongRacketImg, position, Color.White);
        }
    }
}
